/************************************************************
 * Project Name		[Thinking_In_Java]
 * File Name     	[pkg_05_swing.Example_06_Click_Event2.java]
 * Creation Date 	[22-Jun-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_05_swing;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 按钮点击事件2
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Example_06_Click_Event2 {

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				ButtonFrame2 frame = new ButtonFrame2();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

/**
 * 放置按钮面板的窗体
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 */
class ButtonFrame2 extends JFrame {

	private static final long serialVersionUID = 5262965834731194665L;

	private JPanel actionPanel = null;

	/**
	 * 构造方法
	 */
	public ButtonFrame2() {
		this.setTitle("Button Click Event");
		this.setSize(300, 200);
		this.setLocationByPlatform(true);
		this.setResizable(false);

		actionPanel = new JPanel();
		this.generateButton("RED", Color.RED);
		this.generateButton("BLUE", Color.BLUE);
		this.generateButton("YELLOW", Color.YELLOW);

		this.getContentPane().add(actionPanel);
	}

	/**
	 * 生成带有事件监听的按钮
	 * 
	 * @param buttonName
	 *            按钮名称
	 * @param backgroundColor
	 *            背景色
	 */
	private void generateButton(String buttonName, final Color backgroundColor) {
		JButton button = new JButton(buttonName);
		this.actionPanel.add(button);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				actionPanel.setBackground(backgroundColor);
			}
		});
	}

}
